﻿Imports System.Data.SqlClient

Public Class productservice
    Inherits System.Web.UI.Page
    Private objrd As SqlDataReader = Nothing
    Private objfind As SqlDataReader = Nothing
    Private i As Integer
    Private pages As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sSQL As String = ""
        Dim sum, sumdesc As Integer
        Dim objdt As New GetData
        Dim ps As New PreparedStatement(sSQL)
        Dim objdtPro As New GetData
        Dim psPro As New PreparedStatement(sSQL)
        Dim comId As String
        Dim strErr As String = ""
        comId = Request("cid")
        i = Request("page")
        If i = Nothing Or i = 0 Then
            i = 1
        End If
        If comId <> Nothing And comId <> "" Then

            Dim conn As SqlConnection = objdt.getConn()
            ps.addParameter("cid", comId)
            sSQL = "select *  from  merchant_info where company_id=@cid and lang_flag='en'"
            ps.strSql = sSQL
            objrd = objdt.dr(ps, strErr, conn, objrd)
            If Not objrd.Read() Then
                objdt.closeConn(conn)

                Exit Sub
            End If
            title.InnerHtml += ":" & objrd.Item("company_name").ToString
            companyName.InnerText = objrd.Item("company_name").ToString
            sSQL = "select count(*)  from  product_info where company_id=@cid and lang_flag='en'"

            ps.strSql = sSQL
            objrd = objdt.dr(ps, strErr, conn, objrd)
            objrd.Read()
            sum = objrd.Item(0)
            pages = sum / 10 + 1
            If i >= pages Then
                i = pages
            ElseIf i <= 0 Then
                i = 1
            End If
            If (i = pages) Then
                sumdesc = sum
            Else
                sumdesc = i * 10
            End If
            sSQL = "select top(" & i & "*10) * from product_info where company_id=@cid and lang_flag='en' " & _
                    "and product_id not in (" & _
                    "select top (" & i - 1 & "*10) product_id from product_info where company_id=@cid and lang_flag='en' " & _
                    "order by Product_name" & _
                    ") and product_id not in (select top(" & sum - sumdesc & ") product_id from product_info where company_id=@cid and lang_flag='en' " & _
                    "order by Product_name desc) order by Product_name"

            'sSQL = "select top (" & i & "*10) * from  product_info where company_id=@cid and lang_flag='en' and product_id not in (select top ((" & i & "-1)*10) product_id from product_info where company_id=@cid and lang_flag='en')"
            ps.strSql = sSQL
            ps.addParameter("cid", comId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            product.InnerHtml = "<table width=""100%"">"
            Dim photoPath As String
            Dim con As SqlConnection = objdtPro.getConn()
            While (objrd.Read())
                psPro.addParameter("pid", objrd("product_id"))
                sSQL = "select *  from  product_photo where product_id=@pid and photo_type=1"
                psPro.strSql = sSQL


                objfind = objdtPro.dr(psPro, strErr, con, objfind)
                
                While (objfind.Read())
                    If objfind("path").ToString = "" Then
                        photoPath = "/pic/nopic180.gif"
                    Else
                        photoPath = objfind("path").ToString
                    End If
                    product.InnerHtml += "<tr>"
                    product.InnerHtml += "<td width=""200px"" valign=""top""><a href=""productdetail.aspx?productId=" & objrd.Item("product_id") & """><img width=""200px"" height=""150px"" src=""" & photoPath & """ /></a></td>"
                    product.InnerHtml += "<td  valign=""top""><div>" & objrd("Product_name") & "<br/>" & objrd("description") & "</div></td>"
                    product.InnerHtml += "</tr>"
                End While
            End While
            objdtPro.closeConn(con)

            product.InnerHtml += "</table>"
            pageDiv.InnerHtml += "<label  id=""firstPage"" ><a href=""productservice.aspx?cid=" & comId & "&page=1"">First Page</a></label>  "
            pageDiv.InnerHtml += "<label  id=""previousPage"" ><a href=""productservice.aspx?cid=" & comId & "&page=" & i - 1 & """>Previous Page</a></label>  "
            pageDiv.InnerHtml += "<label  id=""nextPage"" ><a href=""productservice.aspx?cid=" & comId & "&page=" & i + 1 & """>Next Page</a></label>  "
            pageDiv.InnerHtml += "<label  id=""lastPage"" ><a href=""productservice.aspx?cid=" & comId & "&page=" & pages & """>Last Page</a></label>"
            objdt.closeConn(conn)
        Else
            Response.Redirect("index.aspx")
        End If
    End Sub

End Class